package com.fstec.ks.master.netty.receiver;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.fstec.ks.master.MasterEngine;
import com.fstec.ks.master.netty.ResponseMessageType;
import com.fstec.ks.master.netty.receiver.bean.ExamRoundBean;
import com.fstec.ks.master.netty.receiver.bean.RoomBean;
import com.fstec.ks.master.netty.receiver.bean.ServerRegBean;
import com.fstec.ks.master.service.ExamDataService;
import com.fstec.ks.master.vo.ExamRoundVO;
import com.fstec.ks.master.vo.ExamServerVO;
import com.fstec.ks.master.vo.RoomRoundVO;
import com.fstec.ks.master.vo.ServerRoundVO;
import com.sinotn.error.MessageException;
import com.sinotn.gson.GsonFacade;
import com.sinotn.ks.definition.Computer;
import com.sinotn.ks.definition.Message;
import com.sinotn.ks.definition.MessageReceiver;
import com.sinotn.ks.netty.message.SimpleMessage;

/**
 * 考试服务器根据授权码注册接入
 * @Title ServerRegReceiver.java
 * @Package com.fstec.ks.master.netty.receiver
 * @Description 
 * Copyright: Copyright (c) 2023
 * Company:广东峰上科技有限公司
 * 
 * @author <a href="mailto:xieqj@fstec.com">谢启进</a>
 * @date 2023年10月12日 下午4:07:14
 * @version V1.0
 */
public class ServerRegReceiver implements MessageReceiver {
	@Autowired
	private MasterEngine masterEngine;
	@Autowired
	private ExamDataService examDataService;
	private int type=101;
	
	@Override
	public int getType() {
		return this.type;
	}

	public void setType(int type) {
		this.type = type;
	}

	@Override
	public void process(Computer computer, Message msg) {
		ServerRegBean bean=GsonFacade.fromJson(msg.getText(), ServerRegBean.class);
		if(masterEngine.getServerInfo().getAuthNum().equals(bean.getAuthNo())){
			List<ExamRoundBean> roundBeans=bean.getRounds();
			ExamRoundBean examRound;
			ExamServerVO examServer=new ExamServerVO();
			int size=roundBeans.size();
			ArrayList<ExamRoundVO> roundVos=new ArrayList<ExamRoundVO>(size);
			ArrayList<ServerRoundVO> serverRounds=new ArrayList<ServerRoundVO>(size);
			ArrayList<RoomRoundVO> roomRounds=new ArrayList<RoomRoundVO>(size);
			List<RoomBean> roomBeans;
			RoomBean roomBean;
			ExamRoundVO examRoundVo;
			ServerRoundVO serverRoundVo;
			RoomRoundVO roomRoundVo;
			String serverName=null;
			int j;
			int roomSize;
			int roundExaminee;
			HashSet<String> roomIdSet=new HashSet<String>();
			this.masterEngine.lockServerReg();
			try{
				for(int i=0;i<size;i++){
					examRound=roundBeans.get(i);
					if(!this.masterEngine.hasRound(examRound.getBeginTime(), examRound.getEndTime())){
						examRoundVo=new ExamRoundVO();
						examRoundVo.setBeginTime(examRound.getBeginTime());
						examRoundVo.setEndTime(examRound.getEndTime());
						roundVos.add(examRoundVo);
					}
					roomBeans=examRound.getRooms();
					roomSize=roomBeans.size();
					roundExaminee=0;
					for(j=0;j<roomSize;j++){
						roomBean=roomBeans.get(j);
						if(serverName==null){
							serverName=roomBean.getName();
						}
						roomIdSet.add(roomBean.getId());
						roomRoundVo=new RoomRoundVO();
						roomRounds.add(roomRoundVo);
						roomRoundVo.setRoundBeginTime(examRound.getBeginTime());
						roomRoundVo.setRoundEndTime(examRound.getEndTime());
						roomRoundVo.setRoomId(roomBean.getId());
						roomRoundVo.setRoomName(roomBean.getName());
						roomRoundVo.setExamineeCount(roomBean.getExamineeCount());
						roundExaminee+=roomBean.getExamineeCount();
					}
					serverRoundVo=new ServerRoundVO();
					serverRounds.add(serverRoundVo);
					serverRoundVo.setRoundBeginTime(examRound.getBeginTime());
					serverRoundVo.setRoundEndTime(examRound.getEndTime());
					serverRoundVo.setPaperDown(examRound.getPaperDown());
					serverRoundVo.setExamineeCount(roundExaminee);
					serverRoundVo.setRoomCount(roomSize);
				}
				examServer.setExamServerName(serverName);
				examServer.setIpAddr(computer.getIpAddr());
				examServer.setServerPort(String.valueOf(bean.getPort()));
				examServer.setRoomCount(roomIdSet.size());
				examServer.setRegTime(new Date());
				
				examDataService.addExamServer(examServer, roundVos, serverRounds, roomRounds);
			}finally{
				this.masterEngine.unlockServerReg();
			}
			SimpleMessage sm=new SimpleMessage(ResponseMessageType.SUCCESS, msg.getToken());
			sm.setText(examServer.getId());
			computer.sendMessage(sm);
			this.masterEngine.reloadData();
		}else{
			// 抛消息异常统一由com.fstec.ks.master.netty.MessageHandler#messageRead处理
			throw new MessageException("注册授权号不正确"+bean.getAuthNo());
		}
	}
}
